from fastapi import APIRouter, HTTPException
from .models import User
from .schemas import UserCreate
from .services import hash_password, verify_password

router = APIRouter()

@router.post("/users/", response_model=UserCreate)
async def create_user(user: UserCreate):
    try:
        user_obj = await User.create(username=user.username, password=user.password,balance=user.balance)
        return user_obj
    except Exception as e:
        raise HTTPException(status_code=400, detail=str(e))

@router.post("/login/")
async def login(username: str, password: str):
    user = await User.get(username=username)
    if user and verify_password(password, user.password):
        return {"message": "Login successful"}
    raise HTTPException(status_code=401, detail="Invalid credentials")
